package com.cxl.live.system.controller;

import com.cxl.live.base.BaseController;
import com.cxl.live.base.ResultModel;
import com.cxl.live.constants.BaseEnums;
import com.cxl.live.system.dto.User;
import com.cxl.live.system.service.UserService;
import com.cxl.live.system.vo.Register;
import com.cxl.live.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Description: UserController
 * 用户Controller
 * @Author: ChenXueLi
 * @Date: 2018-11-10 13:05
 */
@Slf4j
@Api(tags = "用户管理")
@RequestMapping("/sys/user")
@RestController
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "查询所有用户", httpMethod = "POST")
    @PostMapping("/queryAll")
    public ResultModel queryAll(){
        List<User> list = userService.selectAll();
        return ResultUtil.successWithData(list, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @ApiOperation(value = "查找单个用户", httpMethod = "GET")
    @GetMapping("/queryOne/{userId}")
    public ResultModel queryOne(@PathVariable Integer userId){
        User user = userService.get(userId);

        log.info("aaa{}",user.getUserName());

        return ResultUtil.successWithData(user);
    }

    @ApiOperation(value = "添加单个用户", httpMethod = "POST")
    @PostMapping("/save")
    public ResultModel save(@Valid @RequestBody User user){
        user = userService.insertSelective(user);
        return ResultUtil.successWithData(user);
    }

    @ApiOperation(value = "更新单个用户", httpMethod = "POST")
    @PostMapping("/update")
    public ResultModel update(@Valid @RequestBody List<User> user){
        user = userService.persistSelective(user);
        return ResultUtil.successWithData(user);
    }

    @ApiOperation(value = "删除单个用户", httpMethod = "POST")
    @PostMapping("/delete")
    public ResultModel delete(User user){
        userService.delete(user);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除单个用户", httpMethod = "GET")
    @GetMapping("/delete/{userId}")
    public ResultModel delete(@PathVariable Long userId){
        userService.delete(userId);
        return ResultUtil.success();
    }

}
